package com.du.springmvc.controller;

import com.du.springmvc.pojo.Person;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import java.util.Map;

/**
 * @author : DuJiabao
 * @Date : 2021/3/30 18:29
 * @Project : spring-mvc
 * @File : null.java
 * @Desc :
 */
@SessionAttributes("person")
@Controller
public class ResponseController {
  @RequestMapping("/modelAndViewTest")
  ModelAndView modelAndViewTest() {
    ModelAndView mv = new ModelAndView("response");
    mv.addObject("key", "value");
    return mv;
  }

  @RequestMapping("/mapTest")
  String mapTest(Map<String, Object> map) {
    map.put("key", "value");
    return "response";
  }

  @RequestMapping("/modelMapTest")
  String modelMapTest(ModelMap modelMap) {
    modelMap.addAttribute("key", "value");
    return "response";
  }

  @RequestMapping("/modelTest")
  String modelTest(Model model) {
    model.addAttribute("key", "value");
    return "response";
  }

  @RequestMapping("/oneObjectTest")
  String oneObjectTest(Model model, ModelMap modelMap, Map<String, Object> map) {
    System.out.println(model == modelMap);  // true
    System.out.println(model == map);  // true
    System.out.println(model.getClass().getName());  // org.springframework.validation.support.BindingAwareModelMap
    return "response";
  }

  @RequestMapping("/sessionAttributeTest")
  String sessionAttributeTest(Map<String, Object> map) {
    map.put("person", new Person("jack", 20));
    return "session";
  }

  //@ModelAttribute("abc")
  //public Person getPerson() {
  //  return new Person("0", 0);
  //}
  //
  //@ModelAttribute()
  //public void getPerson(Map<String, Object> map) {
  //  map.put("abc", new Person("0", 0));
  //}

  //@RequestMapping("/modelAttributeTest")
  //public String modelAttributeTest(@ModelAttribute("abc") Person abc) {
  //  System.out.println(abc);
  //  return "hello";
  //}

  //@ModelAttribute
  //public Person getPerson() {
  //  return new Person("0", 0);
  //}

  @ModelAttribute()
  public void getPerson(Map<String, Object> map) {
    map.put("person", new Person("0", 0));
  }

  @RequestMapping("/modelAttributeTest")
  public String modelAttributeTest(Person person) {
    System.out.println(person);
    return "hello";
  }
}
